﻿using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Authorization;
using GoodDotNet.WebAdmin.Controllers;
using GoodDotNet.Core.Model;
using GoodDotNet.Service;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.Extensions.Options;
using GoodDotNet.Data;
using GoodDotNet.Core.Data;

namespace GoodDotNet.Webadmin.Controllers
{
    [ApiController]
	[Route("api/[controller]")]
	public class AdminUserRoleController : BaseController
	{
        public AdminUserRoleController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns, ILogger<BaseController> logger, IOptions<Appsettings> appsettings) : base(user, accessor, conns, logger, appsettings)
        {
        }

        [HttpPost]
		[Route("Add")]
		[Authorize]
		public Result Add([FromBody] ParInsertAdminUserRoleModel pars)
		{
		   
		        using (var uniOfWork = new UnitOfWork(_conStr))
		        {
		           AdminUserRoleService service = new AdminUserRoleService(uniOfWork);
		                service.Add(pars);
		        }
		            return Sucess("sucess");
		 
		
		}

        /// <summary>
        /// 用户角色
        /// </summary>
        /// <param name="pars">用户id</param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetListByAdmin")]
        [Authorize]
        public Result GetListByAdmin([FromBody] PostID pars)
        {
            Result resultInfo = new Result();
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminUserRoleService service = new AdminUserRoleService(uniOfWork);
                var roles = service.GetListByAdmin(pars.ID);
                return Sucess(roles);
            }

        }


        [HttpPost]
        [Route("update-user-role")]
        [Authorize]
        public Result UpdateUserRole([FromBody] ParAdminRoleUpdateUserRole pars)
        {
            Result resultInfo = new Result();
            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminUserRoleService service = new AdminUserRoleService(uniOfWork);
                service.UpdateUserRole(pars);
                return Sucess("sucess");
            }

        }

        [HttpPost]
		[Route("Delete")]
		[Authorize]
		public Result Delete([FromBody] ParDeleteAdminUserRoleModel pars)
		{
		  
		        using (var uniOfWork = new UnitOfWork(_conStr))
		        {
		           AdminUserRoleService service = new AdminUserRoleService(uniOfWork);
		                service.Delete(pars);
		        }
		            return Sucess("sucess");
		   
		
		}
		}
}
